package com.blockmeta.bbs.businesslibrary.umeng;

import android.app.Activity;
import android.graphics.Bitmap;
import androidx.annotation.Nullable;
import android.text.TextUtils;

import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.umeng.circle.IMCustom;
import com.blockmeta.bbs.businesslibrary.umeng.circle.IMImage;
import com.blockmeta.bbs.businesslibrary.umeng.circle.IMShare;
import com.blockmeta.bbs.businesslibrary.umeng.circle.IMWeb;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by AirThor
 * Date 2018/1/11.
 */

public class SharedUtils {
    /**
     * 分享长图
     *
     * @param context  上下文
     * @param media    分享的平台
     * @param bitmap   分享的图片文件
     * @param listener 友盟分享监听
     */
    public static void sharePic(Activity context, SHARE_MEDIA media, Bitmap bitmap, UMShareListener listener) {
        sharePic(context, media, bitmap, UMImage.CompressStyle.QUALITY, listener);
    }

    public static void sharePic(Activity context, SHARE_MEDIA media, Bitmap bitmap, UMImage.CompressStyle compressStyle, UMShareListener listener) {
        UMImage image = new UMImage(context, bitmap);
        if (compressStyle != null) {
            image.compressStyle = compressStyle;
        }
        new ShareAction(context)
                .setPlatform(media)
                .withMedia(image)
                .setCallback(listener)
                .share();
    }

    /**
     * 分享网页
     *
     * @param context     上下文
     * @param media       分享
     * @param url         分享的网页链接
     * @param thumb       缩略图
     * @param title       分享标题
     * @param description 分享描述
     */
    public static void shareWeb(Activity context, SHARE_MEDIA media, String url, @Nullable String thumb, @Nullable String title, @Nullable String description) {
        shareWeb(context,media,url,thumb,title,description,null);
    }

    public static void shareWeb(Activity context, SHARE_MEDIA media, String url, @Nullable String thumb, @Nullable String title, @Nullable String description, UMShareListener umShareListener) {
        UMImage umImage;
        if (TextUtils.isEmpty(thumb)||media==SHARE_MEDIA.WEIXIN_CIRCLE) {
            umImage = new UMImage(context, R.drawable.icon_thumb);
        } else {
            umImage = new UMImage(context, thumb);
        }

        title = TextUtils.isEmpty(title) ? "趣会分享" : title;
        description = TextUtils.isEmpty(description) ? "详情见内" : description;
        //友盟分享链接不支持有空格
        String httpUrl = url.trim();
        UMWeb web = new UMWeb(httpUrl);
        web.setTitle(title);
        web.setThumb(umImage);
        web.setDescription(description);

        new ShareAction(context)
        .setPlatform(media)
        .setCallback(umShareListener)
        .withMedia(web)
        .share();
    }

    /**
     * 向IM分享网页
     * @param context 分享发起的页面
     * @param url deeplink跳转使用
     * @param thumb 缩略图
     * @param title 标题
     * @param description 描述
     */
    public static void shareIMWeb(Activity context,String url,@Nullable String thumb,@Nullable String title,@Nullable String description,@Nullable TypeDescribe typeDescribe){
        title = TextUtils.isEmpty(title) ? "趣会分享" : title;
        description = TextUtils.isEmpty(description) ? "详情见内" : description;
        String httpUrl = url.trim();

        IMWeb imWeb = new IMWeb()
        .setTitle(title)
        .setDescribe(description)
        .setTypeDescribe(typeDescribe)
        .setThumb(thumb)
        .setUrl(httpUrl);

        new IMShare(context)
        .withMedia(imWeb)
        .share();
    }

    /**
     * 向IM分享自定义类型消息 如K线 F10等
     * @param context 分享发起页面上下文
     * @param customShareJson 自定义消息的Json字符串
     */
    public static void shareCustom(Activity context,String customShareJson){
        new IMShare(context)
                .withMedia(new IMCustom().setCustomShareJson(customShareJson))
                .share();
    }
    /**
     * 向IM分享图片
     * @param context 分享发起页面
     * @param bitmap 图片
     */
    public static void shareIMImage(Activity context,Bitmap bitmap){
        IMImage imImage = new IMImage()
                .setBitmap(bitmap);
        new IMShare(context)
                .withMedia(imImage)
                .share();
    }
}
